package com.tank.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/9/18 0018
 * Time: 下午 9:54
 * To change this template use File | Settings | File Templates.
 */
public class CharsetTest {

    public static void main(String[] args) throws IOException {

        String input = "input.txt";
        String out = "out.txt";

        RandomAccessFile inputFile = new RandomAccessFile(input,"r");
        RandomAccessFile outFile = new RandomAccessFile(out,"rw");

        File file = new File(input);
        long fileLength = file.length();

        FileChannel inputFileChannel = inputFile.getChannel();
        FileChannel outFileChannel = outFile.getChannel();

       // MappedByteBuffer inputData = inputFileChannel.map(FileChannel.MapMode.READ_ONLY,0,fileLength);
        FileInputStream inputData = new FileInputStream(file);
        byte[] bytes = new byte[512];
        inputData.read(bytes);
        //iso-8859-1
        System.out.println(":"+new String(bytes,"US-ASCII"));
        FileOutputStream outData = new FileOutputStream(out);
        outData.write(bytes);
        outData.flush();
        outData.close();
//        inputData.read(bytes);
//        ByteBuffer buf = ByteBuffer.allocate(512);
//        buf.put(bytes);
//        buf.flip();
//        Charset charset = Charset.forName("iso8859-1");
//        CharsetEncoder charsetEncoder = charset.newEncoder();
//        CharsetDecoder charsetDecoder = charset.newDecoder();
//
//        CharBuffer decodeBuf =charsetDecoder.decode(buf);
//        ByteBuffer encodeBuf = charsetEncoder.encode(decodeBuf);
//        outFileChannel.write(encodeBuf);
//
//        outFileChannel.close();
//        inputFileChannel.close();
    }
}
